/**dao of system notification template
 * by fuzhou huilin corp.ltd 2016.08.30
 * @author lms 2016.08.30
 */
package com.fzrskj.cloud.free.sys.dao;

import com.fzrskj.cloud.free.core.utils.CloudContextUtils;
import com.fzrskj.cloud.free.core.utils.CloudDbDrvUtils;
import com.fzrskj.cloud.free.sys.bean.SysNotifyTemplate;
import com.winiis.component.common.utils.db.DbUtils;
import ms.core.consts.SysErr;
import ms.core.db.IDbDrv;
import ms.core.object.CObject;

import java.util.List;
import java.util.Map;

public class SysNotifyTemplateDao extends CObject{
	//private declarations
	private IDbDrv db=null;

	//constructor function to initialize database access object
	public SysNotifyTemplateDao(){
		db= CloudDbDrvUtils.getDbUtilsDrv();
	}

	//to free resource
	@Override
	public void dispose(){
		db.closeConnection();
	}

	/**
	 * 根据type查询数据
	 *
	 * @param type
	 * @return
	 */
	public List<SysNotifyTemplate> querEmailConfig(int type, int page, int pageSize) {
		String sql = "select * from sys_notify_template where type=? and agentId=? ";
		if (page > 0) {
			sql += String.format("limit %d, %d", (page - 1) * pageSize, pageSize);
		}
		List<SysNotifyTemplate> querEmailConfig = db.queryForBeans(sql, SysNotifyTemplate.class, new Object[]{type, CloudContextUtils.getAgentId()});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return querEmailConfig;
	}

	/**
	 * 查询总数
	 *
	 * @param type
	 * @return
	 */
	public int queryCount(int type) {
		String sql = "select count(*) from sys_notify_template where type=? and agentId=?";
		long counts = (Long) db.queryFieldValue(sql, Long.class, new Object[]{type, CloudContextUtils.getAgentId()}, 1);
		int count = (int) counts;
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return count;
	}

	/**
	 * 根据ID查询表
	 *
	 * @param id
	 * @return
	 */
	public SysNotifyTemplate querEmailById(int id) {
		String sql = "select * from sys_notify_template where id=? and agentId=?";
		SysNotifyTemplate querEmailById = db.queryForBean(sql, SysNotifyTemplate.class, new Object[]{id, CloudContextUtils.getAgentId()});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return querEmailById;
	}

	/**
	 * 根据参数更新数据
	 * @param values
	 * @param cdt
	 * @return
	 */
	public int uptemailconfig(Map<String, Object> values, Map<String, Object> cdt){
		//if (!cdt.containsKey("agentId")) {
			cdt.put("agentId", CloudContextUtils.getAgentId());
		//}
		int ret = db.modifyRecord("sys_notify_template", values, cdt);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to add new record to database
	public int addSysNotifyTemplate(SysNotifyTemplate obj) {
		obj.setState(0);
		obj.setType(1);
		//if (!(obj.getAgentId() > 0)) {
			obj.setAgentId(CloudContextUtils.getAgentId());
		//}
		String sql = "insert into sys_notify_template(tempName,title,content,type,state,agentId) values (?,?,?,?,?,?)";
		Object[] prm = {obj.getTempName(), obj.getTitle(), obj.getContent(), obj.getType(), obj.getState(), CloudContextUtils.getAgentId()};
		db.executeSql(sql, prm);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		} else {
			obj.setId(DbUtils.lastId(this.db));
		}
		this.setErrCode(code);
		return obj.getId();
	}

	//to delete table record by id,only user's template can be deleted
	public int delSysNotifyTemplate(int id) {
		String sql = "delete from sys_notify_template where id=? and type=1 and agentId=?";
		int ret = db.executeSql(sql, new Object[]{id, CloudContextUtils.getAgentId()});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		setErrCode(code);
		return ret;
	}

	//to modify table record by id
	public int modSysNotifyTemplate(int id, SysNotifyTemplate obj) {
		String sql = "update sys_notify_template set " +
				"title=?," +
				"content=?," +
				"tempName=?," +
				"state=? where id=? and type=1 and agentId=?;";
		Object[] prm = {obj.getTitle(), obj.getContent(), obj.getTempName(), obj.getState(), id, CloudContextUtils.getAgentId()};
		int ret = db.executeSql(sql, prm);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		setErrCode(code);
		return ret;
	}

	//to query sys_notify_template item for detail
	public SysNotifyTemplate querySysNotifyTemplate(int id) {
		String sql = "select * from sys_notify_template where id=? and agentId=?";
		SysNotifyTemplate ret = db.queryForBean(sql, SysNotifyTemplate.class, new Object[]{id, CloudContextUtils.getAgentId()});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		setErrCode(code);
		return ret;
	}

	//to query sys_notify_template item for detail
	public SysNotifyTemplate querySysNotifyTemplate(String tempName) {
		String sql = "select * from sys_notify_template where tempName=? and agentId=?";
		SysNotifyTemplate ret = db.queryForBean(sql, SysNotifyTemplate.class, new Object[]{tempName, CloudContextUtils.getAgentId()});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		setErrCode(code);
		return ret;
	}

	/**
	 * 通过代理商获取通知信息.
	 * @param agentId
	 * @param page
	 * @param pageSize
	 * @return
	 */
	public List<SysNotifyTemplate> queryByAgentId(Integer agentId, int page, int pageSize) {
		String sql = "select * from sys_notify_template where agentId=? ";
		if (page > 0) {
			sql += String.format("limit %d, %d", (page - 1) * pageSize, pageSize);
		}
		List<SysNotifyTemplate> querEmailConfig = db.queryForBeans(sql, SysNotifyTemplate.class, new Object[]{agentId});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return querEmailConfig;
	}

	/**
	 * 初始化代理通知配置.
	 * @param agentId 代理ID.
	 * @return affect line number.
	 */
	public Integer initAgent(Integer agentId) {
		String sql = "insert into sys_notify_template (tempName, title, content, type, state, agentId) select " +
				"tempName, title, content, type, state, '" + agentId + "' from sys_notify_template where agentId=0; ";
		Integer affect = this.db.executeSql(sql);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return affect;
	}
}